<?php
class Admin extends Admin_Controller
{
    function __construct() {
        parent::__construct();
    } 
    
    public function index() {
        redirect(site_url('admin/acl/role'));
    }
    
    /**
     * Role  
     */
    public function role() {   
        $this->load->model('role_model'); 
        $roles = $this->role_model->getList();
        $this->title = 'List Role';
        $this->setContent(array('roles' => $roles), 'role');
        $this->template->setLayout();
    }
    
    public function addRole() { 
        $this->load->library('form_validation');
        $validation = array(
            array(
                'field' => 'role_name',
                'label' => 'Role Name',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($validation);
        if($this->form_validation->run()) {
            $data = array(
                'name' => $this->input->post('role_name'),
                'desc' => $this->input->post('role_desc'),
                'active' => $this->input->post('role_active')
            );
            $this->load->model('role_model');
            $this->role_model->add($data);
            redirect(site_url('admin/acl/role'));
        }
        else {
            print_r($this->form_validation->error_string());
        }
        
        $arrVar = array(
            'title_for_layout' => 'Add Role',
            'begin_form' => form_open(base_url() .'admin/acl/addrole', 'post'),
            'end_form' => form_close()
        );
        $this->title = 'Add Role';
        $this->setContent($arrVar, 'role_add');
        unset($arrVar);
        $this->template->setLayout(); 
    }
    
    public function editRole() {
        $this->load->model('role_model');
        $roleInfo = $this->role_model->getRoleInfo($this->uri->segments[4]);
        if(!$roleInfo) {
            redirect(site_url('admin/acl/role'));
        }
        $this->load->library('form_validation');
        $validation = array(
            array(
                'field' => 'role_name',
                'label' => 'Role Name',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($validation);
        if($this->form_validation->run()) {
            $data = array(
                'name' => $this->input->post('role_name'),
                'desc' => $this->input->post('role_desc'),
                'active' => $this->input->post('role_active')
            );
            $this->role_model->update($data, $roleInfo->role_id);
            redirect(site_url('admin/acl/role'));
        }
        else {
            print_r($this->form_validation->error_string());
        }
        
        $arrVar = array(
            'title_for_layout' => 'Edit Role',
            'role' => $roleInfo,
            'begin_form' => form_open(base_url() .'admin/acl/editrole/'. $roleInfo->role_id, 'post'),
            'end_form' => form_close()
        );
        $this->setContent($arrVar, 'role_edit');
        $this->template->setLayout(); 
    }
    
    public function delRole() {
        $this->load->view('role_list'); 
    }
    
    public function active() { 
        $this->load->model('role_model');
        $roleInfo = $this->role_model->getRoleInfo($this->uri->segments[4]);
        if($roleInfo) {
            $this->role_model->active($roleInfo->role_id);
        }
        redirect(site_url('admin/acl/role'));
    }
    
    /**
     * Permission
     */
    public function permission() {
        $this->load->model('permission_model'); 
        $permissions = $this->permission_model->getList();
              
        $arrVar = array(
            'title_for_layout' => 'List Role',
            'permissions' => $permissions
        );
        $this->setContent($arrVar, 'permission');
        $this->template->setLayout();
    }
    
    public function addPermission() {
        $this->load->library('form_validation');
        $validation = array(
            array(
                'field' => 'permission_name',
                'label' => 'Permission Name',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($validation);
        $this->load->model('permission_model');
        if($this->form_validation->run()) {
            $data = array('name' => $this->input->post('permission_name'));
            // Order
            if(trim($this->input->post('permission_order')) != 0) {
                $data['order'] = $this->input->post('permission_order');
            }
            // Code
            if(trim($this->input->post('permission_code')) != '') {
                $data['code'] = strtoupper( $this->input->post('permission_code') );
            }
            
            $this->permission_model->add($data);
            redirect(site_url('admin/acl/permission'));
        }
        else {
            print_r($this->form_validation->error_string());
        }
        $perGroups = $this->permission_model->getGroup();
        $arrVar = array(
            'title_for_layout' => 'Add Permission',
            'begin_form' => form_open(base_url() .'admin/acl/addpermission', 'post'),
            'end_form' => form_close(), 
            'perGroups' => $perGroups
        );
        $this->setContent($arrVar, 'permission_add'); 
        $this->template->setLayout(); 
    }  
    
    public function editPermission() {
        $this->load->library('form_validation');
        $validation = array(
            array(
                'field' => 'permission_name',
                'label' => 'Permission Name',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($validation);
        // Get Info
        $this->load->model('permission_model');
        $permission = $this->permission_model->getInfo($this->uri->segments[4]);
        if(!$permission) {
            redirect(site_url('admin/acl/permission'));
        }
        
        if($this->form_validation->run()) {
            $data = array('name' => $this->input->post('permission_name'));
            // Code
            if(trim($this->input->post('permission_code')) != '') {
                $data['code'] = $this->input->post('permission_code');
            }
            // Order
            if(trim($this->input->post('permission_order')) != 0) {
                $data['order'] = $this->input->post('permission_order');
            }
              
            $result = $this->permission_model->update($data, $permission->per_id);
            if($result) {
                redirect(site_url('admin/acl/permission'));
            }
        }
        else {
            print_r($this->form_validation->error_string());
        }
        // Get group permission
        $perGroups = $this->permission_model->getGroup();           
        $arrVar = array(                                      
            'title_for_layout' => 'Add Permission',
            'begin_form' => form_open(base_url() .'admin/acl/editpermission/'. $permission->per_id, 'post'),
            'end_form' => form_close(),
            'permission' => $permission,
            'perGroups' => $perGroups
        );
        $this->setContent($arrVar, 'permission_edit'); 
        $this->template->setLayout(); 
    } 
    
    public function delPermission() {
        $this->load->view('role_list'); 
    } 
}
?>
